<script lang="ts" setup>
  import { ref, onMounted, onUnmounted } from 'vue';
  import * as echarts from 'echarts/core';
  const props = defineProps({
    dataBar: { type: Object as () => any, required: true },
    dataLine: { type: Object as () => any, required: true },
    datax: { type: Object as () => any, required: true },
    dataName: { type: Object as () => any, default: ['', '', ''] },
  });

  const chartRef = ref<HTMLElement | null>(null);
  let chartInstance: echarts.ECharts | null = null;

  const option = {
    tooltip: {
      trigger: 'axis', // 坐标轴触发 tooltip
      axisPointer: { type: 'cross' }, // 十字准星指示
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true, // 让 grid 包含坐标轴文本
    },
    xAxis: {
      type: 'category',
      data: props.datax ?? [], // X 轴类目
      axisLabel: { color: '#fff' },
      axisLine: { lineStyle: { color: 'rgba(255,255,255,0.6)' } },
      axisTick: { lineStyle: { color: 'rgba(255,255,255,0.6)' } },
    },
    yAxis: [
      {
        type: 'value',
        name: '直方图',
        axisLabel: { color: '#fff' },
        axisLine: { show: true, lineStyle: { color: 'rgba(255,255,255,0.6)' } },
        axisTick: { show: true, lineStyle: { color: 'rgba(255,255,255,0.6)' } },
        splitLine: { show: false },
      },
      {
        type: 'value',
        name: props.dataName[1],
        axisLabel: { color: '#fff' },
        axisLine: { show: true, lineStyle: { color: 'rgba(255,255,255,0.6)' } },
        axisTick: { show: true, lineStyle: { color: 'rgba(255,255,255,0.6)' } },
        splitLine: { show: false },
        position: 'right', // 右侧 Y 轴
      },
    ],
    series: [
      {
        name: props.dataName[0],
        type: 'bar',
        data: props.dataBar ?? [],
        yAxisIndex: 0,
      },
      {
        name: props.dataName[2],
        type: 'line',
        data: props.dataLine ?? [],
        yAxisIndex: 1,
        symbol: 'circle',
        symbolSize: 4,
        smooth: true,
      },
    ],
  };

  const initCharts = () => {
    try {
      if (chartRef.value && !chartInstance) {
        chartInstance = echarts.init(chartRef.value);
        chartInstance?.setOption(option, true);
      }
    } catch (error) {
      console.error('图表初始化失败:', error);
    }
  };

  const updateChart = () => {
    option.series[0].data = props.dataBar ?? [];
    option.series[1].data = props.dataLine ?? [];
    option.xAxis.data = props.datax ?? [];
    chartInstance?.setOption(option, true);
  };

  onMounted(() => {
    // 确保在 DOM 加载完成后更新图表
    setTimeout(() => {
      initCharts();
    }, 100);

    // 添加窗口大小变化监听
    window.addEventListener('resize', updateChart);
  });

  onUnmounted(() => {
    // 移除窗口大小变化监听
    // window.removeEventListener('resize', updateChart);
    window.removeEventListener('resize', handleResize);
    chartInstance?.dispose();
  });

  watch(
    () => [props.datax, props.dataBar, props.dataLine],
    () => updateChart(),
    { deep: true }
  );

  watch(
    () => [props.datax, props.dataBar, props.dataLine],
    () => updateChart(),
    { deep: true }
  );

  defineExpose({
    updateChart, // 暴露方法到父组件
  });

  // 窗口大小变化处理
  const handleResize = () => {
    nextTick(() => {
      chartInstance?.resize();
    });
  };
</script>
<template>
  <div ref="chartRef" class="chart-container">
    <!-- <m-echarts :option="option"></m-echarts> -->
  </div>
</template>

<style scoped>
  .chart-container {
    width: 100%;
    height: 100%;
  }
</style>
